package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    //新增商机跟进记录
    @Transactional
    @Override
    public boolean addBusinessTrack(BusinessTrackVo businessTrackVo) {
        //创建商机记录对象 用来添加商机记录
        TbBusinessTrackRecord trackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,trackRecord);
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        trackRecord.setCreateTime(DateUtils.getNowDate());
        int f1 = businessTrackRecordMapper.insertBusinessTrackRecord(trackRecord);

        //创建商机表用来修改商机
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbBusiness.setId(businessTrackVo.getBusinessId());
        int f2 = tbBusinessMapper.updateTbBusiness(tbBusiness);
        return f1>0 && f2>0;
    }

    //查询商机跟进记录
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        List<TbBusinessTrackRecord> list = businessTrackRecordMapper.list(id);
        /*for (TbBusinessTrackRecord businessTrackRecord : list) {
            String[] items= businessTrackRecord.getKeyItems().split(",");
            for (String item : items) {
                String dictLable= sysDictDataService.selectDictLabel("communication_point",item);
                businessTrackRecord.getKeys().add(dictLable);
            }
        }*/
        return list;
    }


}
